﻿Public Class ExpressionTree
    Public Root As ExpressionNode

    ' Constructor.
    Public Sub New(ByVal expression As String)
        Root = New ExpressionNode(expression.Trim())
    End Sub

    ' Evaluate the parsed expression.
    Public Function Evaluate() As Double
        Return Root.Evaluate()
    End Function

    ' Return the tree's preorder traversal.
    Public Function ShowStack() As String
        Dim result As String = _
            "**********" & vbCrLf
        If Root IsNot Nothing Then
            result &= "> " & Root.Expression & vbCrLf
            result &= Root.ShowStack()
        End If
        Return result
    End Function

    ' Return the tree's rebuilt expression.
    Public Function RebuildExpression() As String
        If Root Is Nothing Then Return ""
        Return Root.RebuildExpression()
    End Function
End Class
